﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Zmjh
{
    public class CourseController : Controller
    {

        #region 1.1竹铭计划课程视图+index()
        /// <summary>
        /// 竹铭计划课程视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {

            return View(); 
       
        }
         #endregion

        #region 获取竹铭课程列表数据 +GetCourseData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取竹铭课程列表数据
        /// </summary>
        /// <retur
        public ActionResult GetCourseData()
        {
            
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);

            var list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetPagedList(pageIndex, pageSize, x => x.Id != null, m => m.Id).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null).Count();
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改竹铭课程 window 的html + EditCourse()
        /// <summary>
        /// 加载修改竹铭课程 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditCourse(int id)
        {
            
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改课程信息 EditCourse(MODEL.qgzx_zm_course model)
        /// <summary>
        ///post修改课程信息 EditCourse(MODEL.qgzx_zm_course model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditCourse(MODEL.qgzx_zm_course model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.Modify(model, "Id", "CourseName", "CourseSubject", "CoursePurpose", "CourseIntroduce", "IsOpen", "IsPoor");
            if (res > 0)
            {
                return Redirect("/Zmjh/Course/index");
            }
            return Redirect("/Zmjh/Course/index");
        }
        #endregion

        #region 在这新增课程 html代码 + AddCourse()
        /// <summary>
        /// 在这新增课程 html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddCourse()
        {

            //SetDropDonwList();
            return PartialView("EditCourse");
        }
        #endregion

        #region 在这新增课程 html代码 + AddCourse(MODEL.qgzx_zm_course model)
        /// <summary>
        /// 在这新增课程 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddCourse(MODEL.qgzx_zm_course model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/Zmjh/Course/index?ok");
            }
            return Redirect("/Zmjh/Course/index?err");
        }
        #endregion

        #region 删除课程 + DelCourse()
        /// <summary>
        /// 删除课程 + DelCourse()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelCourse()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.DelBy(x => x.Id == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion 

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {

            string CourseName = Request.QueryString["CourseName"];//数据筛选的参数
            string name = Request.QueryString["name"];//搜索的参数
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null).ToList();

            if (CourseName != "" && CourseName !=null )
            {
                list = list.Where(x => x.CourseName.Contains(CourseName)).ToList();
            }
            if (name != "" && name != null)
            {
                list = list.Where(x => x.CourseName.Contains(name)).ToList();
            }
           else
            {
                return OperateContext.Current.RedirectAjax("err", "发生错误，请联系管理员解决", null, "");
            }


            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        
    }
}
